//
//  TabbarViewController.m
//  Lextra Junior ueb IT
//
//  Created by Dirk Lieder on 26.05.11.
//  Copyright 2011 Phosphon. All rights reserved.
//

#import "TabbarViewController.h"

#import "Lextra_Engine.h"


@implementation TabbarViewController


@synthesize tab1;
@synthesize tab2;
@synthesize tab3;
@synthesize tab4;
@synthesize myTabBar;

// buttons -> states
typedef enum {
	buttonNO = 0,
	buttonID1,
	buttonID2,
	buttonID3,
	buttonID4
}buttonID;


static UIView *tabBG;


- (void)setDelegate:(id)new_delegate{
	Delegate = new_delegate;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void) setBackgroundImage:(UIImage *)i {

    if(tabBG) [tabBG removeFromSuperview];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:i];
    [myTabBar addSubview:imageView];
    //[myTabBar sendSubviewToBack:imageView];
    tabBG = imageView;
    [imageView release];
    
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
	// set tabbar names
	NSString *theText;
	theText  = NSLocalizedString(@"tabbar1", @"");
	tab1.title = theText;
	theText  = NSLocalizedString(@"tabbar2", @"");
	tab2.title = theText;
	theText  = NSLocalizedString(@"tabbar3", @"");
	tab3.title = theText;
	theText  = NSLocalizedString(@"tabbar4", @"");
	tab4.title = theText;
    [self setStatus:1];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
	NSInteger but = buttonNO;
	
	if(item == tab1) but = buttonID1;	
	if(item == tab2) but = buttonID2;	
	if(item == tab3) but = buttonID3;
	if(item == tab4) but = buttonID4;
	
	switch(but){
            
		case buttonID1:
			[Delegate goScreen:2 theName:@"" thePara:nil deliteView:true];
			break;
            
		case buttonID2:
			[Delegate goScreen:3 theName:@"" thePara:nil deliteView:true];
			break;
			
		case buttonID3:
			[Delegate goScreen:4 theName:@"" thePara:nil deliteView:true];
			break;
			
		case buttonID4:
			[Delegate goScreen:5 theName:@"" thePara:nil deliteView:true];
			break;
			
	}
   [self setStatus:but];
}


- (void)setStatus:(NSInteger)theStatus{	
	//NSLog(@"Hiii1:%d  * ",theStatus);
	switch (theStatus){
			
		case buttonID1:
			myTabBar.selectedItem = tab1;
            [self setBackgroundImage:[UIImage imageNamed:@"tabbar_1.png"]];
			break;
			
		case buttonID2:
			myTabBar.selectedItem = tab2;
            [self setBackgroundImage:[UIImage imageNamed:@"tabbar_2.png"]];
			break;
			
		case buttonID3:
			myTabBar.selectedItem = tab3;
            [self setBackgroundImage:[UIImage imageNamed:@"tabbar_3.png"]];
			break;
			
		case buttonID4:
			myTabBar.selectedItem = tab4;
            [self setBackgroundImage:[UIImage imageNamed:@"tabbar_4.png"]];
			break;
	}
    [[self view] setNeedsDisplay];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
